package com.molichuxing.gateway.bff.user.services.impl;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSONObject;
import com.molichuxing.framework.contants.RedisKeyConst;
import com.molichuxing.framework.utils.Md5Util;
import com.molichuxing.framework.utils.RandomCode;
import com.molichuxing.framework.utils.RandomValidateCodeUtil;
import com.molichuxing.framework.utils.RedisUtil;
import com.molichuxing.gateway.bff.user.entity.request.create.PictureCreateVo;
import com.molichuxing.gateway.bff.user.entity.request.create.SmsCreateVo;
import com.molichuxing.gateway.bff.user.services.SmsBffService;
import com.molichuxing.services.business.service.SmsBizService;
import com.molichuxing.services.property.SmsTemplateEnum;

/**
 * 短信服务接口
 * @author liyg
 * 2019-09-17
 *
 */
@Service("smsBffService")
public class SmsBffServiceImpl implements SmsBffService{

	
	// 环境
	@Value("${profiles.active}")
	private String active;
	@Value("${note.code.life}")
    private String noteCodeLife;
		
	@Resource
	private RedisUtil redisUtil;
	
	@Resource
	private SmsBizService smsBizService;
	
	
	/**
	 * 获取验证码
	 * @param createVo
	 * @return
	 */
	@Override
	public Boolean createSms(SmsCreateVo createVo) {
		validateParams(createVo);
		String code = RandomCode.randomNumber(4);
		Boolean flag=false;
		String userCodeKey = RedisKeyConst.SMS_USER_CODE_LOGIN+Md5Util.md5Hex(active+createVo.getType())+"."+Md5Util.md5Hex(createVo.getPhone());
		if(createVo.getTemplateType() == SmsTemplateEnum.VOICE_CODE.getValue()) {
			flag= smsBizService.sendMessageVoice(createVo.getPhone(), code);
			if(!flag) {
				return flag;
			}
			redisUtil.setex(userCodeKey, code,Integer.valueOf(noteCodeLife));
			return flag;
		}
	    JSONObject json=new JSONObject();
	    json.put("code", code);
	    flag=smsBizService.sendMessage(createVo.getPhone(), createVo.getTemplateType(), json.toJSONString(), null);
	    if(!flag) {
	    	return flag;
	    }
	    redisUtil.setex(userCodeKey, code,Integer.valueOf(noteCodeLife));
		return flag;
	}
	
	/**
	 * 验证参数
	 * @param createVo
	 */
	private void validateParams(SmsCreateVo createVo){
		Assert.notNull(createVo,"参数不能为空");
		Assert.hasLength(createVo.getPhone(), "手机号不能为空");
		createVo.setPhone(createVo.getPhone().trim());
		Assert.notNull(createVo.getTemplateType(),"模板类型不能为空");
		Assert.notNull(createVo.getType(),"类型不能为空");
	}
	
	/**
	 * 获取图形验证码
	 * @param createVo
	 * @return
	 * @throws IOException 
	 */
	@Override
	public String createPictureCode(PictureCreateVo createVo) throws IOException {
		 RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();

         if (null != createVo && createVo.getHeight() > 0) {
             randomValidateCode.setHeight(createVo.getHeight());
         }
         if (null != createVo && createVo.getWidth() > 0) {
             randomValidateCode.setWidth(createVo.getWidth());
         }

         //输出验证码图片方法
         BufferedImage codePic = randomValidateCode.getRandCodePic();
         String picStr = randomValidateCode.getImageBinary(codePic);
         //图片验证码存入redis中
         String usePicrCodeKey = RedisKeyConst.IMG_USER_CODE_LOGIN+Md5Util.md5Hex(active)+"."+Md5Util.md5Hex(createVo.getPhone());
         redisUtil.setex(usePicrCodeKey, randomValidateCode.getCode(),Integer.valueOf(noteCodeLife));
         return picStr;
	}
	
}
